#!/usr/bin/perl

######################################################################
package Swignition::uF::RelEnclosure;
######################################################################

use Swignition::GenericParser::Utils;
use Swignition::uF;
use XML::LibXML qw(:all);
use strict;


sub consume
{
	my $page   = shift;

	# rel-enclosure
	$page->{uF}->{Enclosure} = [ parse_all($page) ];
	foreach my $Enclosure (@{ $page->{uF}->{Enclosure} })
	{
		$page->mergeSubject( $Enclosure->rdf_subject );
		my $val = Swignition::RDFModel::Value->new($Enclosure->uri, 1);
		my $url = $page->uri;
		$page->{RDF}->{$url}->addVal('http://www.iana.org/assignments/relation/enclosure', $val);
	}
	if (defined $page->{uF}->{Enclosure}->[0])
		{ $page->mergeNS( $page->{uF}->{Enclosure}->[0] ); }	
}


sub parse_all
{
	my $page   = shift;
	my $within = shift || $page->{DOM};
	my @nodes = searchRel('enclosure', $within);
	my @rv;
	
	foreach my $t (@nodes)
	{
		my $t_parsed = parse($page, $t);
		push @rv, $t_parsed;
	}
	
	return @rv;
} #/sub parse_all


sub uri
{
	my $this = shift;
	my $all  = shift;
	my @rv;
	
	push @rv, $this->{enclosure};
	
	return $rv[0] unless (defined $all);
	while ($all) { shift @rv; $all--; }
	return @rv;
} #/sub uri


sub metadata_ns
{
	my $this = shift;
	return { 'ianalink' => { nsurl=>'http://www.iana.org/assignments/relation/',title=>'IANA Link Types' } };
} #/sub namespace


sub rdf_subject
{
	my $this    = shift;

	# Create RDF subject
	my $subject = Swignition::RDFModel::Subject->new($this->uri);
	$subject->addObject($this);
	$subject->setDom($this->{_dom});

	if (length $this->{lang})
	{
		my $val = Swignition::RDFModel::Value->new($this->{lang});
		$this->setVocab('http://purl.org/dc/terms/RFC4646');
		$subject->addVal('http://purl.org/dc/terms/language', $val);
	}

	if (length $this->{type})
	{
		my $val = Swignition::RDFModel::Value->new($this->{type});
		$val->setVocab('http://purl.org/dc/terms/IMT');
		$subject->addVal('http://purl.org/dc/terms/format', $val);
	}

	if (length $this->{charset})
	{
		my $val = Swignition::RDFModel::Value->new($this->{charset});
		$subject->addVal('http://purl.org/dc/terms/format', $val);
	}
	
	if (length $this->{media})
	{
		my $val = Swignition::RDFModel::Value->new($this->{media});
		$val->setVocab('http://www.w3.org/TR/CSS21/media.html#media-types');
		$subject->addVal('http://purl.org/dc/terms/format', $val);
	}
	
	return $subject;
	
} #/sub rdf_subject


sub parse
{
	my $page = shift;
	my $rv = { '_dom'=>shift };
	my $pkg = __PACKAGE__;
	if (defined $page->{uF}->{_Shortcuts}->{$pkg}->{ $rv->{_dom}->getAttribute('_xpath') })
		{ return $page->{uF}->{_Shortcuts}->{$pkg}->{ $rv->{_dom}->getAttribute('_xpath') }; }
	else
		{ $page->{uF}->{_Shortcuts}->{$pkg}->{ $rv->{_dom}->getAttribute('_xpath') } = $rv; }
	
	my $t_node = $rv->{'_dom'}->cloneNode(1);

	my $id = $t_node->getAttribute('id');
	$rv->{_id} = $id if (length $id);
	$rv->{_page} = $page;
	
	my $enc = $page->uri($t_node->getAttribute('href'));
	$rv->{enclosure} = $enc;

	my $title = $t_node->getAttribute('title');
	if ($title)
		{ $rv->{label} = $title; }
	else
		{ $rv->{label} = STRINGIFY($t_node, 'value'); }
	
	$rv->{type}    = $t_node->getAttribute('type');
	$rv->{charset} = $t_node->getAttribute('charset');
	$rv->{media}   = $t_node->getAttribute('media');
	$rv->{lang}    = $t_node->getAttribute('hreflang') 
	              || $t_node->getAttribute('lang')
	              || $t_node->getAttribute('xml:lang');
	
	bless $rv;
	return $rv;
	
} #/sub parse


1; #/package Swignition::uF::RelEnclosure
